package cn.nuc.teacher.controller;


import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.nuc.teacher.entity.TeacherEntity;
import cn.nuc.teacher.service.TeacherService;
import cn.nuc.common.utils.PageUtils;
import cn.nuc.common.utils.R;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("teacher")
public class TeacherController {
  @Autowired
  @Qualifier("redisTemplate")
  private RedisTemplate redisTemplate;
  @Autowired
  private TeacherService teacherService;

  private long nextTeacherId;

  //redis 教师列表初始化
  @RequestMapping("/init")
  public R init() {
    // 第一次获取列表从mysql数据库中取出存入redis数据库中
    List<TeacherEntity> list = teacherService.list();
    Iterator iterator = list.iterator();
    for (; iterator.hasNext(); ) {
      TeacherEntity teacher = (TeacherEntity) iterator.next();
      String t = "teacher" + teacher.getTid();
      redisTemplate.opsForValue().set(t, teacher);
      redisTemplate.opsForList().rightPush("teachers", t);

    }
    nextTeacherId = redisTemplate.opsForList().size("teachers");
    return R.ok();
  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("teacher:teacher:list")
  public R list(@RequestParam Map<String, Object> params) {
    List teachersS = redisTemplate.opsForList().range("teachers", 0, redisTemplate.opsForList().size("teachers"));
    List<TeacherEntity> teacherFirstList = redisTemplate.opsForValue().multiGet(teachersS);
    return R.ok().put("teacher", teacherFirstList);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{tid}")
  //@RequiresPermissions("teacher:teacher:info")
  public R info(@PathVariable("tid") String tid) {
//    TeacherEntity teacher = teacherService.getById(tid);
    TeacherEntity teacher = (TeacherEntity) redisTemplate.opsForValue().get("teacher"+tid);
    return R.ok().put("teacher", teacher);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("teacher:teacher:save")
  public R save(@RequestBody TeacherEntity teacher) {
    try{
      TeacherEntity teacherEntity = (TeacherEntity) redisTemplate.opsForValue().get("teacher" + teacher.getTid());
      return R.error();
    }catch (Exception e){
      teacherService.save(teacher);
      redisTemplate.opsForValue().set("teacher"+teacher.getTid(),teacher);
      return R.ok();
    }
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("teacher:teacher:update")
  public R update(@RequestBody TeacherEntity teacher) {
    teacherService.updateById(teacher);
    redisTemplate.opsForValue().set("teacher"+teacher.getTid(),teacher);
    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete/{tid}")
  //@RequiresPermissions("teacher:teacher:delete")
  public R delete(@PathVariable("tid") String tid) {
    teacherService.removeById(tid);
    redisTemplate.opsForValue().set("teacher"+tid,"del",1, TimeUnit.SECONDS);
    redisTemplate.opsForList().remove("teachers",1,"teacher"+tid);
    return R.ok();
  }

  /**
   * 登录
   */
  @RequestMapping("/check")
  //@RequiresPermissions("teacher:teacher:delete")
  public R check(@RequestBody TeacherEntity teacher, HttpSession session) {
//    int tid = teacherService.check(teacher);
//    if (tid == -1) {
//      return R.error();
//    }
//    TeacherEntity teacherEntity = teacherService.getById(tid);
//    session.setAttribute("id", tid);
//    return R.ok().put("teacher", teacherEntity);
    try{
      TeacherEntity teacherEntity = (TeacherEntity) redisTemplate.opsForValue().get("teacher" + teacher.getTid());
      return R.ok().put("student", teacherEntity);
    }catch (Exception e){
      return R.error();
    }
  }

}
